package com.gc.controller.OutExcel;

import java.io.*;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.util.ListUtils;
import com.gc.pojo.OutExcelBook;
import com.gc.pojo.UpExcelStudent;
import com.gc.repository.StudentRepository;
import com.gc.repository.impl.StudentRepositoryImpl;
import com.gc.service.DoExcelService;
import com.gc.service.impl.DoExcelServiceImpl;
import com.gc.util.ExcelWidthStyleStrategy;

/**
 * Servlet implementation class DownExcel
 */
@WebServlet("/DownTestExcel")
public class DownTestExcel extends HttpServlet {

    DoExcelService doExcelService = new DoExcelServiceImpl();
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("上传学生模板文件", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");


        EasyExcel.write(response.getOutputStream()).
                head(head()).sheet("模板").registerWriteHandler(new ExcelWidthStyleStrategy())
                .doWrite(data());
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {



    }

    /**
     *  @ExcelProperty("课程编号")
     *     String courseId;
     *     @ExcelProperty("课程名")
     *     String courseName;
     *     @ExcelProperty("班级名")
     *     String className;
     *     @ExcelProperty("学生号")
     *     String studentId;
     *     @ExcelProperty("学生姓名")
     *     String studentName;
     *     @ExcelProperty("ISBN")
     *     String ISBN;
     *     @ExcelProperty("出版社")
     *     String publisher;
     *     @ExcelProperty("书本名")
     *     String bookName;
     * @return
     */
    private List<List<String>> head() {
        List<List<String>> list = new ArrayList<List<String>>();
        List<String> head0 = new ArrayList<String>();
        head0.add("教学班编号");
        List<String> head1 = new ArrayList<String>();
        head1.add("课程编号");
        List<String> head2 = new ArrayList<String>();
        head2.add("课程名称");
        List<String> head3 = new ArrayList<String>();
        head3.add("学生学号");
        List<String> head4 = new ArrayList<String>();
        head4.add("学生姓名");
        List<String> head5= new ArrayList<String>();
        head5.add("学生性别");
        List<String> head6= new ArrayList<String>();
        head6.add("学生年龄");
        List<String> head7= new ArrayList<String>();
        head7.add("学生手机号");
        List<String> head8= new ArrayList<String>();
        head8.add("学生邮箱");
        list.add(head0);
        list.add(head1);
        list.add(head2);
        list.add(head3);
        list.add(head4);
        list.add(head5);
        list.add(head6);
        list.add(head7);
        list.add(head8);
        return list;
    }
    /**
     * //教学班编号、课程编号、课程名称、学生学号、学生姓名、学生性别、学生年龄、学生手机号、学生邮箱
     * @return
     */
    private List<UpExcelStudent> data() {

        List<UpExcelStudent> list = new ArrayList<>();
        list.add(new UpExcelStudent(
                "755",
                "008",
                "JAVA",
                "s123",
                "刘春霞",
                "女",
                "22",
                "17788333",
                "77233@qq.com"
        ));
        list.add(new UpExcelStudent(
                "注意！请将表格",
                "设置成文本格式",
                "以上都是",
                "测试值",
                null,
                null,
                null,
                null,
                null
        ));
        return list;
    }



}